package com.sdcqjy.property.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sdcqjy.property.R;
import com.sdcqjy.property.activity.DetailsActivity;
import com.sdcqjy.property.mode.ListGroupMode;
import com.sdcqjy.property.mode.ListMsgMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 磁磁帅 on 2018/4/19.
 * 功能：
 */

public class SutraCaseAdapter extends BaseExpandableListAdapter {
    public SutraCaseAdapter(Context context, List<ListGroupMode> modeList) {
        this.context = context;
        this.modeList = modeList;
        inflater = LayoutInflater.from(context);
    }

    private Context context;
    private List<ListGroupMode> modeList;
    private LayoutInflater inflater;

    private ListGroupMode getGroupMode(int groupPosition) {
        int count = 0;
        for (int i = 0; i < modeList.size(); i++) {
            ListGroupMode groupMode = modeList.get(i);
            boolean aNull = modeList.get(i).isNull();
            if (!aNull) {
                if (count == groupPosition)
                    return groupMode;
                count++;
            }
        }
        return null;
    }

    public int getGroupCount() {
        int count = 0;
        for (int i = 0; i < modeList.size(); i++) {
            boolean aNull = modeList.get(i).isNull();
            if (!aNull) count++;
        }
        return count;
    }

    public int getChildrenCount(int groupPosition) {
        ListGroupMode groupMode = getGroupMode(groupPosition);
        if (groupMode == null || groupMode.isNull()) return 0;
        else {
            List<ListMsgMode> modeList = groupMode.modeList;
//            int i = modeList.size() / 2;
//            i += modeList.size() % 2;
//            return i;
            return modeList.size();
        }
    }

    public Object getGroup(int groupPosition) {
        return getGroupMode(groupPosition);
    }

    public Object getChild(int groupPosition, int childPosition) {
        ListGroupMode groupMode = getGroupMode(groupPosition);
        if (groupMode == null) return null;
        else return groupMode.modeList.get(childPosition);
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition << 16 + childPosition;
    }

    public boolean hasStableIds() {
        return false;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_sutra_case_group, null);
            viewHolder = new GroupViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else viewHolder = (GroupViewHolder) convertView.getTag();
        ListGroupMode groupMode = getGroupMode(groupPosition);
        if (groupMode != null) {
            viewHolder.textTitle.setText(groupMode.title);
            viewHolder.icon.setImageResource(groupMode.imgRes);
        }
        return convertView;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_sutra_case_child, null);
            viewHolder = new ChildViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else viewHolder = (ChildViewHolder) convertView.getTag();
        ListGroupMode groupMode = getGroupMode(groupPosition);
        if (groupMode != null) {
            List<ListMsgMode> modeList = groupMode.modeList;
//            int i = childPosition * 2;
//            ListMsgMode mode = modeList.get(i);
            ListMsgMode mode = modeList.get(childPosition);
            viewHolder.setMode1(mode);
//            i++;
//            if (i < modeList.size()) {
//                mode = modeList.get(i);
//                viewHolder.setMode2(mode);
//            }
        }
        return convertView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupViewHolder {
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.textTitle)
        TextView textTitle;

        GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class ChildViewHolder implements View.OnClickListener {
        @BindView(R.id.text1)
        TextView text1;
        //@BindView(R.id.text2)
        //TextView text2;
        private ListMsgMode mode1;

        ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);

        }

        public void setMode1(ListMsgMode mode1) {
            this.mode1 = mode1;
            text1.setText(mode1.title);
        }

        //@OnClick(R.id.text1)
        public void onText1Clicked() {
            if (mode1 != null) DetailsActivity.open(context, mode1);
        }

        public void onClick(View v) {
            onText1Clicked();
        }

        //public void setMode2(ListMsgMode mode2) {
        //this.mode2 = mode2;
        //text2.setText(mode2.title);}
        //@OnClick(R.id.text2)
        //public void onText2Clicked() {
        //if (mode2 != null) DetailsActivity.open(context, mode2);}
    }
}
